package com.mzj.saas.mservice.params.controller;

import com.mzj.saas.mservice.params.entity.SystemParam;
import com.mzj.saas.mservice.params.service.SystemParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Configuration
@Controller
@RequestMapping("/systemParam")
public class SystemParamController {
    private static final Logger LOG = LoggerFactory.getLogger(SystemParamController.class);

    @Autowired
    private SystemParamService systemParamService;
    @RequestMapping(value = "synch", method = RequestMethod.GET)
    @ResponseBody
    public void synch(HttpServletRequest request, HttpServletResponse response, ModelMap map) {
        try {
            systemParamService.synch();
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            try {
                response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
            } catch (IOException e1) {
                LOG.error(e.getMessage(), e1);
            }
        }
    }
    @RequestMapping(value = "saveParam", method = RequestMethod.POST)
    @ResponseBody
    public void create(@RequestBody SystemParam vo, HttpServletRequest request, HttpServletResponse response,
                       ModelMap map) {

        try {
            systemParamService.save(vo);
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);

            try {
                response.sendError(HttpStatus.BAD_REQUEST.value(), e.getMessage());
            } catch (IOException e1) {
                LOG.error(e.getMessage(), e1);
            }
        }
    }
}
